import admin from "@/api/admin";
import {upload} from "@/api/upload";
import dateChoose from "@/utils/datachoose";

export default {
    name: "index",
    created() {
        this.getAllRoles();
        this.searchPage();
    },
    data() {
        //自定义校验规则
        var salaryValid = (rule, value, callback) => {
            setTimeout(() => {
                var b = isNaN(value); //是不是 不是一个数字
                if (b) {
                    callback(new Error('你输入的不是一个数字'));
                } else {
                    callback();
                }

            }, 0);
        };
        return {
            searchForm: {
                currentPage: 1,
                pageSize: 5
            },

            rules: {
                adminAccount: [
                    {required: true, message: '员工账户信息不能空', trigger: 'blur'},
                    {pattern: /^[a-zA-Z]\w{5,8}$/, message: '数字字母下划线组成首位是字母 6-9位', trigger: 'blur'},

                ],
                adminName: [
                    {required: true, message: '员工姓名不能为空', trigger: 'blur'},
                ],
                gender: [
                    {required: true, message: '员工性别不能为空', trigger: 'blur'},
                ],
                adminPhone: [
                    {required: true, message: '手机号不能为空', trigger: 'blur'},
                    {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '手机号格式不正确', trigger: 'blur'},
                ],
                adminEmail: [
                    {required: true, message: '邮箱不能为空', trigger: 'blur'},
                    {type: 'email', message: '邮箱格式不正确', trigger: 'blur'},
                ],
                adminSalary: [
                    {required: true, message: '薪资不能为空', trigger: 'blur'},
                    {validator: salaryValid, trigger: 'blur'},
                ],
                adminCode: [
                    {required: true, message: '身份证号不能为空', trigger: 'blur'},
                    {
                        pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                        message: '18身份证号格式不对',
                        trigger: 'blur'
                    },
                ],
                adminAddress: [
                    {required: true, message: '地址不能为空', trigger: 'blur'},
                ],
            },
            pickerOptions: dateChoose.pickerOptions,
            chooseDate: dateChoose.chooseDate,
            tableData: [],
            total: 0,
            selectIds: [],
            createDialog: false,
            formData: {
                isAdmin: false,
                isActive: true,
                gender: 0
            },
            imgUrl: '',
            roleList: []
        }
    },
    methods: {
        //增删改查操作
        async searchPage() {
            let response = await admin.searchPage(this.searchForm)
            this.total = response.total;
            this.tableData = response.data;
        },

        /**
         * 添加或者修改
         */
        addOrEdit() {
            //表单手动校验
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.createDialog = false;
                    if (this.formData.id) {
                        //修改
                        await admin.update(this.formData);
                    } else {
                        //添加
                        await admin.add(this.formData);
                    }
                    this.searchPage();
                } else {
                    this.$message.error("表单校验失败")
                    return false;
                }
            });
        },

        /*
        * findById
        * */
        async findById(id) {
            this.createDialog = true;
            this.formData = await admin.findById(id);
            this.imgUrl = this.formData.adminAvatar;

        },

        /*
        * 批量删除
        * */

        async batchDeleteByIds() {
            await admin.batchDeleteByIds(this.selectIds);
            this.searchPage();
        },

        /*
        * 通过id删除
        *
        * */
        async deleteById(id) {
            await admin.deleteById(id);
            this.searchPage();
        },

        /**
         * 获取所有角色
         */

        async getAllRoles() {
            this.roleList = await admin.getAllRoles();
        },


        /**
         * 导出表格
         */
        exportExcel() {

        },


        ////////////////////////页面的效果/////////////////////////////////////////
        /**
         * 点击分页回调函数
         */
        pageChange(page) {
            this.searchForm.currentPage = page;
            this.searchPage();
        },
        /**
         * 选择查询时间事件
         */
        chooseDateChange(val) {
            this.searchForm.startTime = val[0];
            this.searchForm.endTime = val[1];
        },
        /**
         * 搜索重置
         */
        resetSearchForm() {
            this.searchForm = {
                currentPage: 1,
                pageSize: 5
            };
            this.chooseDate = ""
        },
        /**
         * 勾选框事件
         */
        selectChange(val) {
            this.selectIds = val.map(item => item.id);
        },
        /**
         * 设置添加按钮的点击事件
         */
        addBtnClick() {
            this.createDialog = true;
            this.formData = {
                isAdmin: false,
                isActive: true,
                gender: 0
            };
            this.imgUrl = ""
            this.$refs.form.resetFields();
        },
        /*
        * 点击批量删除
        * */
        showBatchDeleteDialog() {
            this.$confirm('你确定要删除id为' + this.selectIds + "几个吗", '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                //点击确定
                await this.batchDeleteByIds(this.selectIds)
            })
        },
        /*
        * 文件上传
        * */
        async chooseImg(e) {
            console.log(e.file);
            let formData = new FormData();
            formData.append("avatar", e.file);
            this.imgUrl = await upload(formData);
            this.formData.adminAvatar = this.imgUrl;
        },
        /**
         * 文件上传校验
         */
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 < 500;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG png  格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过500K!');
            }
            return isJPG && isLt2M;
        }

    }
}
